#include "IMaterial.hpp"
#include "../Program/BuiltInShader.hpp"

namespace Material {

MaterialBase::MaterialBase() : _name("Default"), _color(1.0f) {
  if (!_program) {
    _program = Program::ProgramState::CreatProgram();
    // Create a default shader
    _program->InitProgram();
    _program->CreateShaderWithByteArrays(Program::DEFAULT_VERTEX_SHADER.c_str(),
                                         Program::ShaderType::VERTEX);
    _program->CreateShaderWithByteArrays(Program::DEFAULT_FRAGMENT_SHADER.c_str(),
                                         Program::ShaderType::FRAGMENT);
  }
}

MaterialBase::~MaterialBase() { std::cout << _program.use_count() << std::endl; }

MaterialBase::MaterialBase(const Program::ProgramRef &program, const std::string &name)
    : _name(name), _color(1.0f), _program(program) {}

void MaterialBase::Apply() {
  if (!_program->IsBindAttrib()) {
    _program->BindAllAttributes();
  }
  _program->LinkProgram(true);
}

void MaterialBase::UpdateProperties() {
  // TODO: Update Material Properties use another thread
  _program->UseProgram();
  _program->UpdateBuiltInUniform(glm::lookAt(
      glm::vec3(0.0f, 1.0f, 1.0f) * 2.0f, glm::vec3(0.0f), glm::vec3(0.0f, 1.0f, 0.0f)));
}

} // namespace Material